package com.afishing.dao.impl;

import com.afishing.dao.ShopDao;
import com.afishing.entity.Shop;
import com.afishing.utils.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ShopDaoImpl implements ShopDao {
    @Override
    public ArrayList<Shop> getAllShop() {
        try {
            ResultSet resultSet = JDBCUtil.query("select * from shop");
            ArrayList<Shop> shops = new ArrayList<>();
            while(resultSet.next()) {
                Shop shop = new Shop();
                shop.setId(resultSet.getInt("id"));
                shop.setDescription(resultSet.getString("description"));
                shop.setImage(resultSet.getString("image"));
                shop.setName(resultSet.getString("name"));
                shop.setPrice(resultSet.getDouble("price"));
                shop.setAmount(resultSet.getInt("amount"));
                shops.add(shop);
            }
            return shops;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public Shop getShopById(Integer id) {
        try {
            ResultSet resultSet = JDBCUtil.query("select * from shop where id=?", id);
            Shop shop = new Shop();
            while(resultSet.next()) {
                shop.setId(resultSet.getInt("id"));
                shop.setImage(resultSet.getString("image"));
                shop.setDescription(resultSet.getString("description"));
                shop.setName(resultSet.getString("name"));
                shop.setPrice(resultSet.getDouble("price"));
                shop.setAmount(resultSet.getInt("amount"));
            }
            return shop;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void addShop(Shop shop) {
        JDBCUtil.update("insert into shop(name, description, price, amount, image) values(?,?,?,?,?)",
                shop.getName(), shop.getDescription(), shop.getPrice(), shop.getAmount(), shop.getImage());
    }
}
